from lin_base import *

def add_lin_week(paper,x0,y0,s_year,s_month,s_day,e_month,e_day,tz,pra=None,
                 font=unicode_font_36,fgcolor=BLACK,lw=2,draw_rect=None):
    
    dn0 = date_to_days(s_year,s_month,s_day)
    dn_end = date_to_days(s_year,e_month,e_day)
    y=y0
    #pra=None
    zx0=None
    #zx_end=None
    lines=[]
    texts=[]
    pwks=None
    for dn in range(dn0,dn_end+1):
        month,day,year = days_to_date(dn, s_year)
        
        if year != s_year:
            break
        nd = cal_d_w_hm(year,month,day,-tz,0)
        sra, sdec, _ =SunValues(nd)
        ra_hr = sra/15
        if pra is not None:
            while ra_hr < pra:
                ra_hr += 24   # avoid wrap around
        pra = ra_hr        
        
        #print('x0:%s sra:%s ra_hr:%s' % (x0,sra,ra_hr))
        x=RA_to_x(x0, ra_hr)
        
        #print('%s-%s-%s ra:%s x:%s' % (year,month,day,ra_hr,x))
        if x < x0:
            continue
            
        if x > g_share.xr_end:
            break
        #print('x0:%s x:%s ra_hr:%s' % (x0,x,ra_hr))
 
        #wks =date_to_weeks(year, month, day)
        dt = datetime.datetime(year,month,day,0,0,1,0)
        wks = dt.isocalendar()[1]
        #print('%s-%s-%s wks:%s' % (year,month,day,wks))
        if pwks is None:
            pwks=wks
        else:
            if pwks != wks:
                #dt = datetime.datetime(year,month,day,0,0,1,0)
                wd = dt.weekday()
                #WEEKDAYS[wd] 
                #print('wks:%s %s %s' % (wks, wd,WEEKDAYS[wd]  ))
                pwks=wks
                lines.append([(x,y0),(x,y0+YM_MONTH)])
                texts.append(((x,y0+YM_TEXT),'%s' % (wks,)))
                continue
        lines.append([(x,y0),(x,y0+YM_LONG)])        

        #zx_end=x
        #xend=x
    
    #if draw_rect is not None:
    #    draw.rectangle([(zx0,y0),(zx_end,int(y0+Y_MM))],fill=draw_rect)
    #for xys in lines:
    #    draw.line(xys,fill=fgcolor,width=lw)
    #for xytext in texts:
    #    draw.text(xytext[0], xytext[1], font=font,fill=fgcolor)
                
    return pra, zx0, lines, texts
	
	
def add_lin_weeks(paper,title,x0,y0,xend,s_year,font=unicode_font_36,
                    fgcolor=BLACK,lw=2, draw_rect=YELLOW):
    layer = paper.add_layer(name='weeks')
    draw=layer.draw
    zx0=None
    zx1=None
    draw.text((x0-XM_TITLE,y0+YM_TEXT),title, font=font,fill=fgcolor)
    tz=8
    s_month=12
    s_day=20
    e_month=12
    e_day=31
    lines=[]
    texts=[]
    pra,zxx,vlines,vtexts=add_lin_week(paper,x0,y0,s_year,s_month,s_day,e_month,e_day,tz,pra=None,
                 font=font,fgcolor=fgcolor,lw=lw,draw_rect=None)
    lines+= vlines
    texts+= vtexts
    s_year1=s_year+1
    s_month=1
    s_day=1
    e_month=12
    e_day=31
    pra,zxx,vlines,vtexts=add_lin_week(paper,x0,y0,s_year1,s_month,s_day,e_month,e_day,tz,pra=pra,
                 font=font,fgcolor=fgcolor,lw=lw,draw_rect=YELLOW)
    if zxx:
        zx0=zxx
    lines+= vlines
    texts+= vtexts
    s_year2= s_year1+1
    s_month=1
    s_day=1
    e_month=3
    e_day=31
    pra,zxx,vlines,vtexts=add_lin_week(paper,x0,y0,s_year2,s_month,s_day,e_month,e_day,tz,pra=pra,
                 font=font,fgcolor=fgcolor,lw=lw,draw_rect=None)
    if zxx:
        zx1=zxx
    lines+= vlines
    texts+= vtexts
    if draw_rect is not None:
        if zx0 and zx1:
            draw.rectangle([(zx0,y0),(zx1,int(y0+Y_MM))],fill=draw_rect)
            
    for xys in lines:
        draw.line(xys,fill=fgcolor,width=lw)
    for xytext in texts:
        draw.text(xytext[0], xytext[1], font=font,fill=fgcolor)
    
    draw.line([(x0,y0),(xend,y0)],fill=BLACK,width=2)